package com.accenture.portal.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.accenture.portal.model.Link;
import com.accenture.portal.service.impl.LinkServiceImpl;

@Controller
public class LinkController {

	@Autowired
	private LinkServiceImpl lnSer;

	/*
	 * search link
	 */
	@RequestMapping(value = "searchAllLink.action")
	public String searchAllLink(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {

		List<Link> myLink = lnSer.SearchAllLink();
		model.addAttribute("linkList", myLink);

		// model.addAttribute("linkJsonString",new
		// JSONArray().fromObject(myLink).toString());
		return "linkbak";
	}

	@RequestMapping(value = "searchJsonLink.action")
	public String searchJsonLink(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {

		List<Link> myLink = lnSer.SearchAllLink();
		model.addAttribute("linkList", myLink);
		response.getWriter().write(
				new JSONArray().fromObject(myLink).toString());
		response.getWriter().flush();
		return null;
	}

	/*
	 * delete link
	 */

	@RequestMapping(value = "deleteLink.action")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, Model model, Integer link_id) {

		lnSer.DeleteLinkLink(link_id);

		// return "redirect:searchAllLink.action";
		return "redirect:searchAllLink.action";
	}

	/*
	 * update link
	 */
	// finish editing and to save
	@RequestMapping(value = "updatelink.action")
	public String upgateLink(HttpServletRequest request,
			HttpServletResponse response, Model model, Link link)
			throws Exception {
		lnSer.updateLink(link);
		// return "redirect:searchAllLink.action";
		return "redirect:searchAllLink.action";
	}

	// go to editing page(update)
	@RequestMapping(value = "goToeditLink.action")
	public String EditLink(Model model, int link_id) {
		Link editLink = lnSer.SearchLinkByLinkId(link_id);
		model.addAttribute("editLink", editLink);
		return "editLink";
	}

	/*
	 * 
	 * add link
	 */

	// finish add link and submit
	@RequestMapping(value = "addLink.action")
	public String AddLink(HttpServletRequest request,
			HttpServletResponse response, Model model, Link link) {

		lnSer.AddLink(link);

		// return "redirect:searchAllLink.action";
		return "redirect:searchAllLink.action";

	}

	// go to add link
	@RequestMapping(value = "goToAddLink.action")
	public String GotoAddLink(HttpServletRequest request,
			HttpServletResponse response, Model model, Link link) {
		return "addLink";
	}
	
	@RequestMapping(value = "searchQuickLink.action")
	public String searchQuickLink(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {

		List<Link> quickLink = lnSer.SearchQuickLink();
		model.addAttribute("quickLinkList", quickLink);
		response.getWriter().write(
				new JSONArray().fromObject(quickLink).toString());
		response.getWriter().flush();
		return null;
	} 
	
	

	public LinkServiceImpl getLnSer() {
		return lnSer;
	}

	public void setLnSer(LinkServiceImpl lnSer) {
		this.lnSer = lnSer;
	}

	/**
	 * 通过link名进行模糊查询
	 * @author chuyan.qi
	 * @param linkname
	 * @param response
	 * @param model
	 * @throws Exception 
	 */
	@RequestMapping("searchLinkByName.action")
	public void searchLinkByName(String linkname, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		List<Link> linklist = lnSer.searchLinkByName("%" + linkname + "%");
		String text = JSONArray.fromObject(linklist).toString();
		PrintWriter out = response.getWriter();
		out.write(text);
	}
}
